<!DOCTYPE HTML>
<html lang="en">
<head>
<title>{...} (block) - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="Blocks are one or more statements enclosed in braces. Typically used with function definitions and control flow statements." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>{...} (block)</h1>
<p>Blocks are one or more <a href="../Concepts.htm#statement">statements</a> enclosed in braces. Typically used with <a href="../Functions.htm#define">function definitions</a> and <a href="../Language.htm#control-flow">control flow statements</a>.</p>
<pre class="Syntax">
{
    <i>Statements</i>
}</pre>

<h2>Remarks</h2>
<p>A block is used to bind two or more <a href="../Concepts.htm#statement">statements</a> together. It can also be used to change which <a href="../Language.htm#if-statement">If statement</a> an <a href="Else.htm">Else statement</a> belongs to, as in this example where the block forces the Else statement to belong to the first If statement rather than the second:</p>
<pre>if (Var1 = 1)
{
    if (Var2 = "abc")
        Sleep, 1
}
else
    return</pre>
<p>Although blocks can be used anywhere, currently they are only meaningful when used with <a href="../Functions.htm#define">function definitions</a>, <a href="../Language.htm#if-statement">If statements</a>, <a href="Else.htm">Else</a>, <a href="../Language.htm#loop-statement">Loop statements</a>, <a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a> or <a href="Finally.htm">Finally</a>.</p>
<p>If any of the control flow statements mentioned above has only a single statement, that statement need not be enclosed in a block (this does not work for function definitions). However, there may be cases where doing so enhances the readability or maintainability of the script.</p>
<p>A block may be empty (contain zero statements), which may be useful in cases where you want to comment out the contents of the block without removing the block itself.</p>
<p id="otb"><strong>One True Brace (OTB, K&amp;R style):</strong> The OTB style may optionally be used in the following places: <a href="../Functions.htm#define">function definitions</a>, <a href="IfExpression.htm">If (expression)</a>, <a href="Else.htm">Else</a>, <a href="Loop.htm">Loop Count</a>, <a href="While.htm">While</a>, <a href="For.htm">For</a>, <a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a>, and <a href="Finally.htm">Finally</a>. This style puts the block's opening brace on the same line as the block's controlling statement rather than underneath on a line by itself. For example:</p>
<pre>MyFunction(x, y) {
    ...
}
if (x &lt; y) {
    ...
} else {
    ...
}
Loop %RepeatCount% {
    ...
}
While x &lt; y {
    ...
}
For k, v in obj {
    ...
}
Try {
    ...
} Catch e {
    ...
} Finally {
    ....
}</pre>
<p>Similarly, a statement may exist to the right of a brace (except the open-brace of the One True Brace style). For example:</p>
<pre>if (x = 1)
{ MsgBox This line appears to the right of an opening brace. It executes whenever the IF-statement is true.
    MsgBox This is the next line.
} MsgBox This line appears to the right of a closing brace. It executes unconditionally.</pre>

<h2>Related</h2>
<p><a href="../Functions.htm#define">Function Definitions</a>, <a href="../Language.htm#control-flow">Control Flow Statements</a>, <a href="../Language.htm#if-statement">If Statements</a>, <a href="Else.htm">Else</a>, <a href="../Language.htm#loop-statement">Loop Statements</a>, <a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a>, <a href="Finally.htm">Finally</a></p>

<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>if (x = 1)
{
    MsgBox, test1
    Sleep, 5
}
else
    MsgBox, test2</pre>
</div>

</body>
</html>
